/**
 * MIT License
 * 
 * Copyright (c) 2017 CaiDongyu
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.tunnel.server.tcp;

import java.util.HashMap;
import java.util.Map;

import com.tunnel.common.tunnel.TunnelDataQueueManager;
import com.tunnel.server.tcp.TcpClientManager.TcpClient;

import io.netty.channel.ChannelHandlerContext;

public class TcpChannelManager {
	
	public static class TcpChannelContext{
		private int port;
		private String connectionId;
		private ChannelHandlerContext channel;
		
		public int getPort() {
			return port;
		}
		public void setPort(int port) {
			this.port = port;
		}
		public String getConnectionId() {
			return connectionId;
		}
		public void setConnectionId(String connectionId) {
			this.connectionId = connectionId;
		}
		public ChannelHandlerContext getChannel() {
			return channel;
		}
		public void setChannel(ChannelHandlerContext channel) {
			this.channel = channel;
		}
		
	}
	
	private static Map<String,TcpChannelContext> CHANNEL_MAP = new HashMap<>();

	public synchronized static boolean add(int port,String connectionId,ChannelHandlerContext ctx){
		if(CHANNEL_MAP.containsKey(connectionId)){
			return false;
		}
		
//		LogUtil.log("新的连接: "+requestId);
		TcpChannelContext tcpCtx = new TcpChannelContext();
		tcpCtx.setPort(port);
		tcpCtx.setConnectionId(connectionId);
		tcpCtx.setChannel(ctx);
		CHANNEL_MAP.put(connectionId, tcpCtx);
		return true;
	}
	
	public synchronized static TcpChannelContext get(String connectionId){
		return CHANNEL_MAP.get(connectionId);
	}

	public static void remove(String connectionId) {
//		LogUtil.log("断开tcp连接: "+connectionId);
		TcpChannelContext ctx = CHANNEL_MAP.remove(connectionId);
		
		//通知tunnel-client，不需要再继续传递数据给这个requestId
		if(ctx != null){
			TcpClient tcpClient = TcpClientManager.get(ctx.getPort());
			if(tcpClient != null){//连接还在
				ChannelHandlerContext clientCtx = tcpClient.getChannelHandlerContext();
				byte[] connectionIdBytes = connectionId.getBytes();
				TunnelDataQueueManager.commitData(TunnelDataQueueManager.TCP_CLOSE_MSG, clientCtx, connectionIdBytes);
			}
		}
	}
}
